package com.leyou.item.controller;

import com.leyou.common.pojo.PageResult;
import com.leyou.item.pojo.SpuBo;
import com.leyou.item.service.GoodsService;
import com.mysql.cj.x.protobuf.Mysqlx;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("spu")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @RequestMapping("page")
    public ResponseEntity<PageResult<SpuBo>> queryGoodsByPage(
            @RequestParam(value = "key",required = false) String key,
            @RequestParam(value = "saleable",required = false) Boolean saleable,
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "5") Integer rows
    ){
        PageResult<SpuBo> spuBoPageResult = goodsService.queryGoodsByPage(key, saleable, page, rows);
        if(spuBoPageResult==null||CollectionUtils.isEmpty(spuBoPageResult.getItems())){
            return ResponseEntity.notFound().build();
        }else{
            return ResponseEntity.ok(spuBoPageResult);
        }
    }
    // @GetMapping("detail/{spuId}")
    // public ResponseEntity<SpuBo> queryGoodsById(@PathVariable("spuId") Long spuId){
    //
    // }
    @PostMapping("goods")
    public ResponseEntity<Void> insertGoods(@RequestBody SpuBo spuBo){
        this.goodsService.insertGoods(spuBo);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

}
